package com.czp.ssm.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.czp.ssm.bean.Example;
import com.czp.ssm.bean.Role;
import com.czp.ssm.bean.User;
import com.czp.ssm.service.RoleService;
import com.czp.ssm.service.UserService;
/**
 * 权限控制器
 * @author 柴泽鹏
 *
 */

@Controller
@RequestMapping("/shiroController")
public class ShiroController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	
	/**
	 * 没有授权的页面
	 * @return
	 */
	@RequestMapping("/toUnauthorizedUrl")
	public String toUnauthorizedUrl(){
		return "foreground/user/login";
	}
	
	
	@RequestMapping(value="/logout" , method=RequestMethod.GET)
	public String logout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "forward:/planeController/toPlaneIndex";
	}
	
	/**
	 * 注册
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/register" , method=RequestMethod.POST)
	public String register(User user) {
		System.out.println("user----"+user);
		userService.register(user);
		
		return "forward:/shiroController/toUnauthorizedUrl";
	}
	
	/**
	 * 登陆
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/login" , method=RequestMethod.POST)
	public String login(User user) {
		Subject subject = SecurityUtils.getSubject();
		if (!subject.isAuthenticated()) {
			UsernamePasswordToken token = new UsernamePasswordToken(user.getUserPhone(),user.getUserPassword());
			token.setRememberMe(true);
			try {
				subject.login(token);
			} catch (UnknownAccountException uae) {  
	            return "error username";  
	        } catch (IncorrectCredentialsException ice) {  
	            return "error password";  
	        }  
		}
		return "forward:/planeController/toPlaneIndex";
	}
	
	
	/**
	 * 登陆
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/toAdminIndex" , method=RequestMethod.POST)
	public String toAdminIndex(User user) {
		Subject subject = SecurityUtils.getSubject();
		if (!subject.isAuthenticated()) {
			UsernamePasswordToken token = new UsernamePasswordToken(user.getUserPhone(),user.getUserPassword());
			token.setRememberMe(true);
			try {
				subject.login(token);
			} catch (UnknownAccountException uae) {  
	            return "error username";  
	        } catch (IncorrectCredentialsException ice) {  
	            return "error password";  
	        }  
		}
		return "/background/index";
	}
	
	
	/**
	 * 用户管理
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/userManage")
	public ModelAndView userManage() {
		List<User> users = userService.findAllUser();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/background/admin/userRoleList");
		modelAndView.addObject("users", users);
		return modelAndView;
	}
	
	
	/**
	 * 角色管理
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/roleManage")
	public ModelAndView roleManage() {
		List<Role> roles = roleService.findAllRole();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/background/admin/roleMenuList");
		modelAndView.addObject("roles", roles);
		return modelAndView;
	}
	
	
}
